package com.example.uidemo_01.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.example.uidemo_01.R;

public class CheckBoxActivity extends AppCompatActivity  {
    /*第一种方法*/
//    implements CompoundButton.OnCheckedChangeListener
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);

        //绑定控件
        CheckBox ck1 = findViewById(R.id.ck1);
        CheckBox ck2 = findViewById(R.id.ck2);
        CheckBox ck3 = findViewById(R.id.ck3);
        //设置监听 第一种方法
    //        ck1.setOnCheckedChangeListener(this);
//        ck2.setOnCheckedChangeListener(this);
//        ck3.setOnCheckedChangeListener(this);


/*第二种方法*/
        ck1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //判断是否选中，并提示
                if(isChecked){
                    Toast.makeText(CheckBoxActivity.this, buttonView.getText()+"被选中", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(CheckBoxActivity.this, buttonView.getText()+"被取消选中", Toast.LENGTH_SHORT).show();
                }
            }
        });
        ck2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //判断是否选中，并提示
                if(isChecked){
                    Toast.makeText(CheckBoxActivity.this, buttonView.getText()+"被选中", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(CheckBoxActivity.this, buttonView.getText()+"被取消选中", Toast.LENGTH_SHORT).show();
                }
            }
        });
        ck3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //判断是否选中，并提示
                if(isChecked){
                    Toast.makeText(CheckBoxActivity.this, buttonView.getText()+"被选中", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(CheckBoxActivity.this, buttonView.getText()+"被取消选中", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
//第一中方法
//    @Override
//    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//        //判断是否选中，并提示
//        if(isChecked){
//            Toast.makeText(this, buttonView.getText()+"被选中", Toast.LENGTH_SHORT).show();
//        }else{
//            Toast.makeText(this, buttonView.getText()+"被取消选中", Toast.LENGTH_SHORT).show();
//        }
//
//    }
}
